package com.app.service.impl;

import com.app.mapper.*;
import com.app.model.*;
import com.app.service.FormService;
import com.app.util.CommonHelp;
import com.app.util.DateUtil;
import com.app.vo.FormVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
//import sun.applet.Main;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Primary
public class FormServiceImpl implements FormService {
    @Autowired
    FormMapper formMapper;

    @Autowired
    AppealMapper appealMapper;

    @Autowired
    FormRemarkMapper formRemarkMapper;
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    RoadMapper roadMapper;

    @Override
    public FormModel getFormById(int id) {
        QueryWrapper<FormModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("id", id);
        FormModel plantModel = formMapper.selectOne(queryWrapper);
        List<DepartmentModel> departmentModels = departmentMapper.selectList(null);
        for (int i1 = 0; i1 < departmentModels.size(); i1++) {
            if (plantModel.getDepId() != null && Objects.equals(plantModel.getDepId(), departmentModels.get(i1).getId())) {
                plantModel.setDepText(departmentModels.get(i1).getName());
            }
        }
        if (plantModel == null) {
            return null;
        } else {
            return plantModel;
        }
    }

    @Override
    public Page<FormModel> getPageFormList(int page, int limit, Integer userId, Integer status) {
        QueryWrapper<FormModel> queryWrapper = new QueryWrapper<>();
        if (userId != null) {
            queryWrapper.select().eq("user_id", userId);
        }
        if (status != null && status != 9) {
            if (status == 0 || status == 3) {
                queryWrapper.select().eq("status", status);
            } else {
                queryWrapper.ne("status", 0);
                queryWrapper.ne("status", 3);
            }
        }
        if (userId != null && status == 9) {
            queryWrapper.select().eq("user_id", userId);
        }
        queryWrapper.select();
        queryWrapper.orderByDesc("id");
        Page<FormModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<FormModel> plantList = formMapper.selectPage(pages, queryWrapper);
        return plantList;
    }

    @Override
    public FormVo getPageByModel(FormModel model) {
        model.setPage((model.getPage() - 1) * model.getLimit());
        if (model.getSelectCrtTime() != null && !model.getSelectCrtTime().equals("")) {
            String[] split = model.getSelectCrtTime().split(",");
            model.setStartTime(split[0] + " 00:00:00");
            model.setEndTime(split[1] + " 23:59:59");
        }
        List<FormModel> formList = formMapper.getBehindFormList(model);
        List<DepartmentModel> departmentModels = departmentMapper.selectList(null);
        List<CategoryModel> categoryModels = selectTermList();
        for (int i = 0; i < formList.size(); i++) {
            for (int i1 = 0; i1 < categoryModels.size(); i1++) {
                if (formList.get(i).getTermId() != null && Objects.equals(formList.get(i).getTermId(), categoryModels.get(i1).getId())) {
                    formList.get(i).setTermText(categoryModels.get(i1).getName());
                }
            }
        }
        for (int i = 0; i < formList.size(); i++) {
            for (int i1 = 0; i1 < departmentModels.size(); i1++) {
                if (formList.get(i).getDepId() != null && Objects.equals(formList.get(i).getDepId(), departmentModels.get(i1).getId())) {
                    formList.get(i).setDepText(departmentModels.get(i1).getName());
                }
            }
        }
        int count = formMapper.getBehindFormCount(model);
        FormVo vo = new FormVo();
        vo.setList(formList);
        vo.setCount(count);
        return vo;
    }

    @Override
    public List<FormModel> getModelByUserId(Integer userId) {
        QueryWrapper<FormModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select();
        if (userId != null) {
            queryWrapper.eq("user_id", userId);
        }

        List<FormModel> plantModel = formMapper.selectList(queryWrapper);
        if (plantModel == null) {
            return null;
        } else {
            return plantModel;
        }
    }

    @Override
    public void update(FormModel model) {
        formMapper.updateById(model);
    }

    @Override
    public void insert(FormModel model) {
        formMapper.insert(model);
    }

    @Override
    public void delete(int id) {
        formMapper.deleteById(id);
    }

    @Override
    public Integer getModelTodayCount(Boolean register) {
        QueryWrapper<FormModel> queryWrapper = new QueryWrapper<>();
        if (register) {
            queryWrapper.eq("status", 3);//目前没数据先定义3为立案受理
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(CommonHelp.getNowTimestamp());
        queryWrapper.like("status_time", time);
        return formMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer getModelYesterdayCount(Boolean register) {
        QueryWrapper<FormModel> queryWrapper = new QueryWrapper<>();
        if (register) {
            queryWrapper.eq("status", 3);//目前没数据先定义3为立案受理
        }
        Date date = new Date(CommonHelp.getNowTimestamp().getTime());
        Date yDate = DateUtil.addDays(date, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(yDate);
        queryWrapper.like("status_time", time);
        return formMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer getModelByQuestionTypeCount(Integer questionType, Integer status) {
        QueryWrapper<FormModel> queryWrapper = new QueryWrapper<>();
        if (questionType != null) {
            queryWrapper.eq("question_type", questionType);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        return formMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer getModelCountByTime(String crtTime, Integer questionType) {
        QueryWrapper<FormModel> queryWrapper = new QueryWrapper<>();
        if (questionType != null) {
            queryWrapper.eq("question_type", questionType);
        }
        queryWrapper.apply("TO_DAYS(crt_time) = TO_DAYS('" + crtTime + "')");
        return formMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer getModelCountByTimeAndStatus(String crtTime, Integer status) {
        QueryWrapper<FormModel> queryWrapper = new QueryWrapper<>();
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.apply("TO_DAYS(crt_time) = TO_DAYS('" + crtTime + "')");
        return formMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer getModelCount(Integer status) {
        QueryWrapper<FormModel> queryWrapper = new QueryWrapper<>();
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        return formMapper.selectCount(queryWrapper);
    }

    @Override
    public List<CategoryModel> selectTypeList() {
        return formMapper.selectTypeList();
    }

    @Override
    public List<RoadModel> road() {
        List<RoadModel> roadModel = formMapper.selectRoad();
        for (int i = 0; i < roadModel.size(); i++) {
            List<RoadModel> roadModels = formMapper.selectStreet(roadModel.get(i).getId());
            roadModel.get(i).setStreet(roadModels);
        }
        return roadModel;
    }

    @Override
    public List<RoadModel> get() {
        return formMapper.get();
    }

    @Override
    public FormVo getSuperintendList(FormModel model) {
        model.setPage((model.getPage() - 1) * model.getLimit());
        if (model.getSelectCrtTime() != null && !model.getSelectCrtTime().equals("")) {
            String[] split = model.getSelectCrtTime().split(",");
            model.setStartTime(split[0] + " 00:00:00");
            model.setEndTime(split[1] + " 23:59:59");
        }
        List<FormModel> formList = formMapper.getSuperintendList(model);
        List<DepartmentModel> departmentModels = departmentMapper.selectList(null);
        List<CategoryModel> categoryModels = selectTermList();
        for (int i = 0; i < formList.size(); i++) {
            for (int i1 = 0; i1 < categoryModels.size(); i1++) {
                if (formList.get(i).getTermId() != null && Objects.equals(formList.get(i).getTermId(), categoryModels.get(i1).getId())) {
                    formList.get(i).setTermText(categoryModels.get(i1).getName());
                }
            }
        }
        for (int i = 0; i < formList.size(); i++) {
            for (int i1 = 0; i1 < departmentModels.size(); i1++) {
                if (formList.get(i).getDepId() != null && Objects.equals(formList.get(i).getDepId(), departmentModels.get(i1).getId())) {
                    formList.get(i).setDepText(departmentModels.get(i1).getName());
                }
            }
        }
        List<RoadModel> road = get();
        for (int i = 0; i < formList.size(); i++) {
            for (int i1 = 0; i1 < road.size(); i1++) {
                if (formList.get(i).getRoad() != null && Objects.equals(formList.get(i).getRoad(), road.get(i1).getId())) {
                    formList.get(i).setRoadText(road.get(i1).getName());
                }
                if (formList.get(i).getStreet() != null && Objects.equals(formList.get(i).getStreet(), road.get(i1).getId())) {
                    formList.get(i).setStreetText(road.get(i1).getName());
                }
            }
        }
        int count = formMapper.getCount(model);
        FormVo vo = new FormVo();
        vo.setList(formList);
        vo.setCount(count);
        return vo;
    }

    @Override
    public List<CategoryModel> selectTermList() {
        return formMapper.selectTermList();
    }

    @Override
    public void insertAppeal(AppealModel model) {
        appealMapper.insert(model);
    }

    @Override
    public AppealModel selectAppeal(Integer id) {
        QueryWrapper<AppealModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("form_id", id);
        return appealMapper.selectOne(queryWrapper);

    }

    @Override
    public void insertHandle(FormRemarkModel model) {
        model.setCrtTime(CommonHelp.getNowTimestamp());
        formRemarkMapper.insert(model);
    }

    @Override
    public FormRemarkModel selectHandle(Integer id) {
        QueryWrapper<FormRemarkModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("form_id", id);
        FormRemarkModel formRemarkModels = formRemarkMapper.selectOne(queryWrapper);
        if (formRemarkModels == null) {
            return null;
        }
        if (formRemarkModels.getResultUrl() != null) {
            String[] split = formRemarkModels.getResultUrl().split(",");
            formRemarkModels.setUrl(split);
        }
        return formRemarkModels;
    }

    @Override
    public FormRemarkModel selectRemark(Integer formId) {
        return formRemarkMapper.selectRemark(formId);
    }

    @Override
    public void updateHandle(FormRemarkModel model) {
        model.setUpTime(CommonHelp.getNowTimestamp());
        formRemarkMapper.updateById(model);
    }

    @Override
    public List<AppealModel> getAppeal() {
        return formMapper.getAppeal();
    }

    @Override
    public AppealModel selectAppealById(Integer formId) {
        return formMapper.selectAppealById(formId);
    }

    @Override
    public void deleteAppeal(Integer formId) {
        QueryWrapper<AppealModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("form_id", formId);
        appealMapper.delete(queryWrapper);
    }

    @Override
    public Integer selectCount(Integer departmentId) {
        QueryWrapper<FormModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dep_id", departmentId);
        return formMapper.selectCount(queryWrapper);
    }

    @Override
    public List<FormModel> selectList(Integer id) {
        QueryWrapper<FormModel> queryWrapper = new QueryWrapper<>();
        if (id == 1) {
            queryWrapper.isNull("superintend");
        }
        if (id == 2) {
            queryWrapper.isNotNull("superintend");
        }
        return formMapper.selectList(queryWrapper);
    }

    @Override
    public List<RoadAndStreetModel> getRoad() {
        QueryWrapper<RoadAndStreetModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("pid", 0);
        return roadMapper.selectList(queryWrapper);
    }

    @Override
    public List<RoadAndStreetModel> getStreet() {
        QueryWrapper<RoadAndStreetModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 0);
        return roadMapper.selectList(queryWrapper);
    }

    @Override
    public void updateAppeal(AppealModel model) {
        appealMapper.updateById(model);
    }
}
